#include "cwit_string.h"
#include "string.h"
cwit_list *cwit_string_split_to_list(char *data,char *splite)
{
    cwit_list *strList = cwit_list_init();
    char *fontp = data;
    while(fontp != NULL)
    {
        int substrlen = 0;
        int offset = 0;
        char *ss =  strstr(fontp,splite);//首次出现字串的位置
        if(ss == NULL)
        {
            offset = 0;
            substrlen = strlen(fontp)+1;
        }
        else {
            substrlen = ss-fontp+1;
            offset = strlen(splite);
        }
        
        char *str = malloc(substrlen);
        memset(str,0,substrlen);
	    memcpy(str,fontp,substrlen);
        str[substrlen-1] = '\0';
		cwit_list_push_tail(strList,str);
        fontp = ss+offset;
    }
    return strList;
}

static int hexCharToInt(char c) {
    if (c >= '0' && c <= '9') {
        return c - '0';
    } else if (c >= 'a' && c <= 'f') {
        return c - 'a' + 10;
    } else if (c >= 'A' && c <= 'F') {
        return c - 'A' + 10;
    } else {
        return -1;
    }
}

// 将字符串转换为十六进制数组
int  stringToHex(const char *input, unsigned char *output) {
    size_t length = strlen(input);
    if (length % 2 != 0) {
        return -1;
    }

    for (size_t i = 0; i < length; i += 2) {
        if(hexCharToInt(input[i]) >= 0 &&  hexCharToInt(input[i + 1]) >=0 )
        {
            output[i / 2] = (hexCharToInt(input[i]) << 4) | hexCharToInt(input[i + 1]);
        }
    }
    return 1;
}